package com.learn.ch02;

import com.alibaba.druid.pool.DruidDataSource;
import com.learn.ch02.pojo.App;
import com.learn.ch02.pojo.MyDataSource;
import com.learn.ch02.pojo.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.TransactionManager;

import java.util.Date;
@SpringBootApplication
//@EnableConfigurationProperties({User.class, App.class})
//@ConfigurationPropertiesScan("com.learn.ch02.pojo")
@ImportResource("classpath:spring.xml")
public class Ch02InitializrApplication {
	@Bean
	public Date getDate() {
		return new Date();
	}

	public static void main(String[] args) {
		// Ch02InitializrApplication.class 对象又被称为源，SpringBoot从这个配置类开始加载项目中所有的bean
		ConfigurableApplicationContext applicationContext = SpringApplication.run(Ch02InitializrApplication.class, args);
		// 获取自动配置的sqlSessionFactory对象
		// 这同时是不使用junit的单元测试的方式
		Object sqlSessionFactory = applicationContext.getBean("sqlSessionFactory");
		System.out.println(sqlSessionFactory);
		// 也可以传入.class对象指定类型
		Object transactionManager = applicationContext.getBean(TransactionManager.class);
		System.out.println(transactionManager);
		// 关闭服务器
		applicationContext.close();
	}
	// 将配置属性绑定到第三方bean
	@Bean
	@ConfigurationProperties("spring.datasource")
	public DruidDataSource getDruidDataSource() {
        return new DruidDataSource();
	}
}
